--[[

LPS_Data.lua

Data definition and data handling routines.

]]

function LPS_DataDefine()
	if (LPS_Config == nil) then
		LPS_Config = {};
	end
	
	if (LPS_Config ~= nil) then
		LPS_DataConvert();
	end

	-- Keyring Sort Options
	if (LPS_Config.keyring == nil) then
		LPS_Config.keyring = 0;
	end

	-- Backback Sort Options
	if (LPS_Config.pack0 == nil) then
		LPS_Config.pack0 = 0;
	end

	-- Pack 1 Sort Options
	if (LPS_Config.pack1 == nil) then
		LPS_Config.pack1 = 0;
	end

	-- Pack 2 Sort Options
	if (LPS_Config.pack2 == nil) then
		LPS_Config.pack2 = 0;
	end

	-- Pack 3 Sort Options
	if (LPS_Config.pack3 == nil) then
		LPS_Config.pack3 = 0;
	end

	-- Pack 3 Sort Options
	if (LPS_Config.guild == nil) then
		LPS_Config.guild = 0;
	end
	
	-- Pack 4 Sort Options
	if (LPS_Config.pack4 == nil) then
		LPS_Config.pack4 = 0;
	end

	if (LPS_Config.lowestfirst == nil) then
		LPS_Config.lowestfirst = true;
	end

	if (LPS_Config.autosort == nil) then
		LPS_Config.autosort = false;
	end

	-- Herb Sorting
	if (LPS_Config.herbsort == nil) then
		LPS_Config.herbsort = -1;
	end

	-- Mineral Sorting
	if (LPS_Config.mineralsort == nil) then
		LPS_Config.mineralsort = -1;
	end

	-- Leather Sorting
	if (LPS_Config.leathersort == nil) then
		LPS_Config.leathersort = -1;
	end

	-- Cloth Sorting
	if (LPS_Config.clothsort == nil) then
		LPS_Config.clothsort = -1;
	end

	-- Consumables Sorting
	if (LPS_Config.consumablesort == nil) then
		LPS_Config.consumablesort = -1;
	end

	-- Other Trade Goods
	if (LPS_Config.tradesort == nil) then
		LPS_Config.tradesort = -1;
	end

	-- Gems Sorting
	if (LPS_Config.gemsort == nil) then
		LPS_Config.gemsort = -1;
	end

	-- Custom Sorting 1
	if (LPS_Config.custom1sort == nil) then
		LPS_Config.customsort1 = -1;
	end

	-- Custom Sorting 2
	if (LPS_Config.custom2sort == nil) then
		LPS_Config.customsort2 = -1;
	end

	-- Quest Item Pack Sorting
	if (LPS_Config.questPack == nil) then
		LPS_Config.questPack = -1;
	end

	-- Soulbound Item Pack Sorting
	if (LPS_Config.sbitemPack == nil) then
		LPS_Config.sbitemPack = -1;
	end

	-- Manual Sort List 1
	if (LPS_Config.SortList_1 == nil) then
		LPS_Config.SortList_1 = {};
	end

	-- Manual Sort List 2
	if (LPS_Config.SortList_2 == nil) then
		LPS_Config.SortList_2 = {};
	end
end


-- Function: Convert old data to per-characters version, erase global data for player once converted.
function LPS_DataConvert()

	-- Get the player name.
	local LPS_PLAYER = LPS_SF_GetPlayerRealmName();

	if (LPS_Config[LPS_PLAYER] == nil) then
		return;
	end

	if (LPS_Config[LPS_PLAYER].pack0 ~= nil) then
		LPS_Config.pack0 = LPS_Config[LPS_PLAYER].pack0;
	end

	if (LPS_Config[LPS_PLAYER].pack1 ~= nil) then
		LPS_Config.pack1 = LPS_Config[LPS_PLAYER].pack1;
	end

	if (LPS_Config[LPS_PLAYER].pack2 ~= nil) then
		LPS_Config.pack2 = LPS_Config[LPS_PLAYER].pack2;
	end

	if (LPS_Config[LPS_PLAYER].pack3 ~= nil) then
		LPS_Config.pack3 = LPS_Config[LPS_PLAYER].pack3;
	end

	if (LPS_Config[LPS_PLAYER].pack4 ~= nil) then
		LPS_Config.pack4 = LPS_Config[LPS_PLAYER].pack4;
	end

	if (LPS_Config[LPS_PLAYER].lowestfirst ~= nil) then
		LPS_Config.lowestfirst = LPS_Config[LPS_PLAYER].lowestfirst;
	end

	if (LPS_Config[LPS_PLAYER].autosort ~= nil) then
		LPS_Config.autosort = LPS_Config[LPS_PLAYER].autosort;
	end

	if (LPS_Config[LPS_PLAYER].questPack ~= nil) then
		LPS_Config.questPack = LPS_Config[LPS_PLAYER].questPack;
	end

	-- Erase old data so we never convert it again.
	LPS_Config[LPS_PLAYER] = {};
	LPS_Config[LPS_PLAYER] = nil;

end

-- Function: Set the configuration options to a "default" setting.
function LPS_DataDefaults()
	DEFAULT_CHAT_FRAME:AddMessage(LPS_MSG_SETDEFAULTS);

	if (LPS_Config == nil) then
		LPS_DataDefine();
	end

	LPS_Config.lowestfirst		= true;
	LPS_Config.autosort		= false;

	-- Special Item Sorting
	LPS_Config.herbsort		= 2;
	LPS_Config.mineralsort		= 2;
	LPS_Config.leathersort		= 2;
	LPS_Config.clothsort		= 2;
	LPS_Config.consumablesort	= 1;
	LPS_Config.tradesort		= 2;
	LPS_Config.gemsort		= 2;
	LPS_Config.questPack		= 3;
	LPS_Config.sbitemPack		= -1;

	LPS_Config.pack0		= 1;
	LPS_Config.pack1		= 2;
	LPS_Config.pack2		= 2;
	LPS_Config.pack3		= 2;
	LPS_Config.pack4		= 2;

	LPS_Config.packb0		= 2;
	LPS_Config.packb1		= 2;
	LPS_Config.packb2		= 2;
	LPS_Config.packb3		= 2;
	LPS_Config.packb4		= 2;
	LPS_Config.packb5		= 2;
	LPS_Config.packb6		= 2;
	LPS_Config.packb7		= 2;

	LPS_Config.guild		= 0;

	-- Update the GUI options pane
	LPS_GUI_UpdateValues();
end
